clear
set more off
cd "$statadir"

cap log close
log using "$logdir/ertc_histogram.log", replace

forval i = 0/1 {

    if `i' == 0 {
        local k ertc
    }
    else {
        local k ertc + ertc_amendment
    }

    use $fromsasdir/result if `k' > 0 ///
        & num_emp > 0 & wg_amt/num_emp > 20, clear

    gen post = tp >= 202103
    gen ratio = (`k')/wg_amt
    gen rbin = min(100, round(ratio*50)*2)

    gcollapse (count) n = firm_id, by(rbin post) fast
    sort post rbin
    by post: gegen ntot = sum(n)
    gen f = n/ntot
    qui replace rbin = rbin/100
    
    gen i = `i'
    tempfile d`i'
    qui save `d`i'', replace
}
clear
forval i = 0/1 {
    append using `d`i''
}
local tstr0 "2020Q2-2020Q4"
local tstr1 "2021Q1-2021Q4"

qui sum n
assert r(min) >= 5

forval post = 0/1 {
    twoway scatter f rbin if post == `post' & i == 0, mcolor(gs0) || ///
        scatter f rbin if post == `post' & i == 1, mcolor(gs8) msymbol(circle_hollow) ///
            xtitle("Ratio of ERTC to total wages") ///
            ytitle("Density") ///
            scheme(s1color) ///
            legend(order(1 "On original return" 2 "On original or amended return") cols(1)) ///
            title("`tstr`post''") 
            
    graph save g`post'.gph, replace
}
grc1leg g0.gph g1.gph, ycommon leg(g0.gph) scheme(s1color)
graph display, ysize(9) xsize(16)


qui save $outdir/ertc_histogram, replace



cap log close


